import {Component, Input, OnInit} from '@angular/core';
import {Dept} from "../../../../model/Dept";
import {ManageService} from "../../../../services/manage.service";
import {AlertService} from "../../../../services/alert.service";

@Component({
  selector: 'app-bmedit',
  templateUrl: './bmedit.component.html',
  styleUrls: ['./bmedit.component.css']
})
export class BmeditComponent implements OnInit{
  @Input() optype:string;
  @Input() pid:string;
  @Input() id:string;
  d:Dept;
  constructor(private manageService:ManageService
    ,private alertservice:AlertService) {
    this.d=new Dept();
  }

  ngOnInit(): void {
    if(this.optype=='add'){
      this.d=new Dept();
      this.d.pid=this.pid
    }
    else if(this.optype=='update'){
      this.getDeptByid();
    }
  }
  save(){
    this.manageService.saveDept(this.d)
      .then((data:any)=>{
        if(data.msg=='ok'){

        }
        else {
          this.alertservice.showError('保存部门信息异常，请重试');
        }
      })
  }

  getDeptByid(){
    this.manageService.getSingleDept(this.id)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.d=new Dept();
          this.d=data.obj;
        }
        else {
          this.alertservice.showError('获取部门信息异常,请重试');
        }
      })
  }

}
